/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package data_bases.business;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import others.DAO.ExecuteStatementDAO;

/**
 *
 * @author chucho
 */
public class DataBaseBusiness {
    private ResultSet resultSet;

    public DataBaseBusiness(String SQL) {
        resultSet = new ExecuteStatementDAO().doSQL(SQL);
    }
    public DataBaseBusiness(){}
    public DataBaseBusiness(ResultSet resultSet){
        this.resultSet = resultSet;
    }
    public List<String> getCollate(){
        List<String> collates = new ArrayList<>();
        try {
            while(resultSet.next()){
                collates.add(resultSet.getString(1));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return collates;
    }
    public List<Schema> getSchemas(){
        List<Schema> schemas = new ArrayList<>();
        try {
            while(resultSet.next()){
                schemas.add(new Schema(resultSet.getString(1)));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return schemas;
    }
    public List<String> getSchemasString(){
        List<String> schemas = new ArrayList<>();
        try {
            while(resultSet.next()){
                schemas.add(resultSet.getString(1));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return schemas;
    }
    public List<ArrayList<String>> getTable(){
        List<ArrayList<String>> StringList2 = new ArrayList<>();
        try{
        while(resultSet.next()){
            ArrayList<String> StringList = new ArrayList<>();
            for (int i = 1; i <= resultSet.getMetaData().getColumnCount();i++){
                StringList.add(resultSet.getString(i));
            }
            StringList2.add(StringList);
        }
        }catch(Exception e){
            e.printStackTrace();
        }
        return StringList2;
    }
    
    public List<String> getColumnName() {
        List<String> stringList = new ArrayList<>();
        try {
            for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
                stringList.add(resultSet.getMetaData().getColumnName(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringList;
    }
    /////////////////////////////////
    public List<String> getDatabaseTables(String database){
        List<String> tables = new ArrayList();
        try {
            ResultSet tablesSet = new data_bases.DAO.DatabaseDAO().databaseTables(database);
            while(tablesSet.next()){
                tables.add(tablesSet.getString(1));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tables;
    }
    public List<String> getDatabaseFunctions(String database){
        List<String> functions = new ArrayList();
        try {
            ResultSet functionSet = new data_bases.DAO.DatabaseDAO().databaseFunctions(database);
            while(functionSet.next()){
                functions.add(functionSet.getString(2));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return functions;
    }
    public List<String> getDatabaseTriggers(String database){
        List<String> triggers = new ArrayList();
        try {
            ResultSet triggerSet = new data_bases.DAO.DatabaseDAO().databaseTriggers(database);
            while(triggerSet.next()){
                triggers.add(triggerSet.getString(1));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return triggers;
    }
    public List<String> getDatabaseProcedures(String database){
        List<String> procedures = new ArrayList();
        try {
            ResultSet procedureSet = new data_bases.DAO.DatabaseDAO().databaseProcedures(database);
            while(procedureSet.next()){
                procedures.add(procedureSet.getString(2));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return procedures;
    }
    public List<String> getDatabaseIndex(String database){
        List<String> index = new ArrayList();
        List<String> dataBaseTables = getDatabaseTables(database);
        for(String table : dataBaseTables){
            try {
                ResultSet resultset = new data_bases.DAO.DatabaseDAO().tableIndex(table, database);
                while(resultset.next()){
                    index.add(resultset.getString(3)+"-> Tabla "+table);
                }
            } catch (SQLException ex) {
                Logger.getLogger(DataBaseBusiness.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return index;
    }
    //////////////////////////////////////////////////
}
